from django.shortcuts import render
from rest_framework import views
from rest_framework import authentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from utils.permissions import IsOwnerOrReadOnly
from apps.note.models import NoteModel
from apps.note.serilalizers import NoteSerializers
from rest_framework.response import Response
from rest_framework import status
from rest_framework import mixins
from rest_framework import views, viewsets


# Create your views here.

class UploadTextView(APIView):
    authentication_classes = (authentication.SessionAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)

    def post(self, request):
        user = self.request.user
        content = request.data['content']
        if user and content:
            note = NoteModel()
            note.user = user
            note.content = content
            print(note)
            note.save()
            return Response(data=request.data, status=status.HTTP_200_OK)

        return Response(status=status.HTTP_401_UNAUTHORIZED)


class GetTextView(mixins.ListModelMixin, viewsets.GenericViewSet):
    """获取文本内容"""
    authentication_classes = (authentication.SessionAuthentication, JSONWebTokenAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)

    def get_queryset(self):
        query = NoteModel.objects.filter(user=self.request.user)
        print(query, "@222")
        return query
    serializer_class = NoteSerializers

